package cn.edu.scau.cmi.hombio.service;

import cn.edu.scau.cmi.hombio.dao.MedicineStorageDAO;
import cn.edu.scau.cmi.hombio.dao.MedicineSupplierDAO;
import cn.edu.scau.cmi.hombio.dao.SupplierDAO;

import cn.edu.scau.cmi.hombio.domain.MedicineStorage;
import cn.edu.scau.cmi.hombio.domain.MedicineSupplier;
import cn.edu.scau.cmi.hombio.domain.Supplier;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for Supplier entities
 * 
 */

@Service("SupplierService")

@Transactional
public class SupplierServiceImpl implements SupplierService {

	/**
	 * DAO injected by Spring that manages MedicineStorage entities
	 * 
	 */
	@Autowired
	private MedicineStorageDAO medicineStorageDAO;

	/**
	 * DAO injected by Spring that manages MedicineSupplier entities
	 * 
	 */
	@Autowired
	private MedicineSupplierDAO medicineSupplierDAO;

	/**
	 * DAO injected by Spring that manages Supplier entities
	 * 
	 */
	@Autowired
	private SupplierDAO supplierDAO;

	/**
	 * Instantiates a new SupplierServiceImpl.
	 *
	 */
	public SupplierServiceImpl() {
	}

	/**
	 * Save an existing MedicineSupplier entity
	 * 
	 */
	@Transactional
	public Supplier saveSupplierMedicineSuppliers(Integer id, MedicineSupplier related_medicinesuppliers) {
		Supplier supplier = supplierDAO.findSupplierByPrimaryKey(id, -1, -1);
		MedicineSupplier existingmedicineSuppliers = medicineSupplierDAO.findMedicineSupplierByPrimaryKey(related_medicinesuppliers.getId());

		// copy into the existing record to preserve existing relationships
		if (existingmedicineSuppliers != null) {
			existingmedicineSuppliers.setId(related_medicinesuppliers.getId());
			related_medicinesuppliers = existingmedicineSuppliers;
		} else {
			related_medicinesuppliers = medicineSupplierDAO.store(related_medicinesuppliers);
			medicineSupplierDAO.flush();
		}

		related_medicinesuppliers.setSupplier(supplier);
		supplier.getMedicineSuppliers().add(related_medicinesuppliers);
		related_medicinesuppliers = medicineSupplierDAO.store(related_medicinesuppliers);
		medicineSupplierDAO.flush();

		supplier = supplierDAO.store(supplier);
		supplierDAO.flush();

		return supplier;
	}

	/**
	 */
	@Transactional
	public Supplier findSupplierByPrimaryKey(Integer id) {
		return supplierDAO.findSupplierByPrimaryKey(id);
	}

	/**
	 * Return a count of all Supplier entity
	 * 
	 */
	@Transactional
	public Integer countSuppliers() {
		return ((Long) supplierDAO.createQuerySingleResult("select count(o) from Supplier o").getSingleResult()).intValue();
	}

	/**
	 * Delete an existing MedicineSupplier entity
	 * 
	 */
	@Transactional
	public Supplier deleteSupplierMedicineSuppliers(Integer supplier_id, Integer related_medicinesuppliers_id) {
		MedicineSupplier related_medicinesuppliers = medicineSupplierDAO.findMedicineSupplierByPrimaryKey(related_medicinesuppliers_id, -1, -1);

		Supplier supplier = supplierDAO.findSupplierByPrimaryKey(supplier_id, -1, -1);

		related_medicinesuppliers.setSupplier(null);
		supplier.getMedicineSuppliers().remove(related_medicinesuppliers);

		medicineSupplierDAO.remove(related_medicinesuppliers);
		medicineSupplierDAO.flush();

		return supplier;
	}

	/**
	 * Save an existing Supplier entity
	 * 
	 */
	@Transactional
	public void saveSupplier(Supplier supplier) {
		Supplier existingSupplier = supplierDAO.findSupplierByPrimaryKey(supplier.getId());

		if (existingSupplier != null) {
			if (existingSupplier != supplier) {
				existingSupplier.setId(supplier.getId());
				existingSupplier.setName(supplier.getName());
			}
			supplier = supplierDAO.store(existingSupplier);
		} else {
			supplier = supplierDAO.store(supplier);
		}
		supplierDAO.flush();
	}

	/**
	 * Delete an existing MedicineStorage entity
	 * 
	 */
	@Transactional
	public Supplier deleteSupplierMedicineStorages(Integer supplier_id, Integer related_medicinestorages_id) {
		MedicineStorage related_medicinestorages = medicineStorageDAO.findMedicineStorageByPrimaryKey(related_medicinestorages_id, -1, -1);

		Supplier supplier = supplierDAO.findSupplierByPrimaryKey(supplier_id, -1, -1);

		related_medicinestorages.setSupplier(null);
		supplier.getMedicineStorages().remove(related_medicinestorages);

		medicineStorageDAO.remove(related_medicinestorages);
		medicineStorageDAO.flush();

		return supplier;
	}

	/**
	 * Delete an existing Supplier entity
	 * 
	 */
	@Transactional
	public void deleteSupplier(Supplier supplier) {
		supplierDAO.remove(supplier);
		supplierDAO.flush();
	}

	/**
	 * Load an existing Supplier entity
	 * 
	 */
	@Transactional
	public Set<Supplier> loadSuppliers() {
		return supplierDAO.findAllSuppliers();
	}

	/**
	 * Return all Supplier entity
	 * 
	 */
	@Transactional
	public List<Supplier> findAllSuppliers(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<Supplier>(supplierDAO.findAllSuppliers(startResult, maxRows));
	}

	/**
	 * Save an existing MedicineStorage entity
	 * 
	 */
	@Transactional
	public Supplier saveSupplierMedicineStorages(Integer id, MedicineStorage related_medicinestorages) {
		Supplier supplier = supplierDAO.findSupplierByPrimaryKey(id, -1, -1);
		MedicineStorage existingmedicineStorages = medicineStorageDAO.findMedicineStorageByPrimaryKey(related_medicinestorages.getId());

		// copy into the existing record to preserve existing relationships
		if (existingmedicineStorages != null) {
			existingmedicineStorages.setId(related_medicinestorages.getId());
			existingmedicineStorages.setTime(related_medicinestorages.getTime());
			related_medicinestorages = existingmedicineStorages;
		} else {
			related_medicinestorages = medicineStorageDAO.store(related_medicinestorages);
			medicineStorageDAO.flush();
		}

		related_medicinestorages.setSupplier(supplier);
		supplier.getMedicineStorages().add(related_medicinestorages);
		related_medicinestorages = medicineStorageDAO.store(related_medicinestorages);
		medicineStorageDAO.flush();

		supplier = supplierDAO.store(supplier);
		supplierDAO.flush();

		return supplier;
	}
}
